#! /bin/sh
#
### BEGIN INIT INFO
# Provides:          kopano-server
# Required-Start:    $syslog $network $remote_fs
# Required-Stop:     $syslog $network $remote_fs
# Should-Start:      mysql
# Should-Stop:       mysql
# Default-Start:     2 3 5
# Default-Stop:      0 1 6
# X-UnitedLinux-Default-Enabled:   yes
# Short-Description: Kopano Core Storage Server
### END INIT INFO

. /etc/rc.status
rc_reset

SERVERCONFIG=/etc/kopano/server.cfg
SERVERPROGRAM=/usr/sbin/kopano-server

# Sanity checks.
#[ -f $SERVERCONFIG ] || exit 0
[ -x $SERVERPROGRAM ] || exit 0

SERVERCONFIG_OPT=""
[ ! -z $SERVERCONFIG -a -f $SERVERCONFIG ] && SERVERCONFIG_OPT="-c $SERVERCONFIG"

[ -f /etc/sysconfig/kopano ] && . /etc/sysconfig/kopano
export KOPANO_USERSCRIPT_LOCALE
if [ -z "$KOPANO_LOCALE" ]; then
	KOPANO_LOCALE="C"
fi

server=`basename $SERVERPROGRAM`

case "$1" in
    start)
		echo -n "Starting the Server "
		install -dm0775 -o kopano -g kopano /var/run/kopano
		export LC_ALL=$KOPANO_LOCALE
		export LANG=$KOPANO_LOCALE
		$SERVERPROGRAM $SERVERCONFIG_OPT
		rc_status -v
		unset LC_ALL LANG
		;;

    stop)
		if [ -f /tmp/zarafa-upgrade-lock -o -f /tmp/kopano-upgrade-lock ]; then
			echo
			echo "Server database upgrade is taking place."
			echo "Do not stop this process bacause it may render your database unusable."
			echo
			exit 1
		fi
		echo -n "Stopping server "
		killproc $server -TERM
		rc_status -v
		;;

    restart)
		$0 stop
		$0 start
		;;

	reload)
		echo -n "Restarting the Server: "
		killproc $server -SIGHUP
		rc_status -v
		;;

    status)
		echo -n "Checking for the Server "
		checkproc $server
		rc_status -v
		;;

    *)
		;;
esac

rc_exit
